package kines.ct;

import kines.*;
import kines.SerializableDB;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.ArrayList;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class EventType extends Catalog implements SerializableDB {
  /**
   * Default contructor
   */
  public EventType( ){

  }

  public static int getId( String eventType )throws Exception{
    int idR;
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_phase = ConnRs.createStatement( );
    String str_phase = "select id_evento from evento_descrip where descripcion_evento = '"+(eventType!=null?eventType.trim():"")+"'";
    ResultSet rs_phase = stmt_phase.executeQuery( str_phase );
    if( rs_phase.next() ){
      idR = rs_phase.getInt("id_evento");
    }else{
      idR = -1;
    }
    ConnRs.close();
    return idR;
  }
  public static EventType getData(int eventTypeId)throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_eventTypes = ConnRs.createStatement( );
    String str_eventTypes = "select id_evento,descripcion_evento from evento_descrip where id_evento = "+eventTypeId;
    ResultSet rs_eventTypes = stmt_eventTypes.executeQuery( str_eventTypes );
    EventType et = null;
    if( rs_eventTypes.next() ){
      et = new EventType( );
      et.setId( rs_eventTypes.getInt("id_evento") );
      et.setDescription( rs_eventTypes.getString("descripcion_evento") );
    }
    ConnRs.close();
    return et;
  }
  public static Object[] getData()throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    ArrayList eventTypes = new ArrayList();
    Statement stmt_eventTypes = ConnRs.createStatement( );
    String str_eventTypes = "select id_evento,descripcion_evento from evento_descrip";
    ResultSet rs_eventTypes = stmt_eventTypes.executeQuery( str_eventTypes );
    while( rs_eventTypes.next() ){
      EventType et = new EventType( );
      et.setId( rs_eventTypes.getInt("id_evento") );
      et.setDescription( rs_eventTypes.getString("descripcion_evento") );
      eventTypes.add(et);
    }
    ConnRs.close();
    return eventTypes.toArray();
  }
  public void saveData(HttpSession session) throws Exception {
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_insert_event_type = ConnRs.createStatement( );
    String str_insert_event_type = "";
    if( id > 0 ){
      str_insert_event_type = "update evento_descrip set descripcion_evento = '"+description+"' where id_evento = "+id;
    }else{
      str_insert_event_type = "insert into evento_descrip (descripcion_evento) values ('"+description+"')";
    }
    stmt_insert_event_type.execute(str_insert_event_type);
    session.setAttribute( "mensaje", "Tipo de Evento fue guardado con exito" );
    ConnRs.close();
  }
  public boolean deleteData() throws Exception {
    boolean ret = true;
    if( id > 0 ){
      Driver DriverRs = (Driver)Class.forName(driver).newInstance();
      Connection ConnRs = DriverManager.getConnection(strconn,username,password);
      Statement stmt_delete_event_type = ConnRs.createStatement( );
      String str_delete_event_type = "delete from evento_descrip where id_evento = "+id+"";
      int totBorrados = stmt_delete_event_type.executeUpdate(str_delete_event_type);
      ConnRs.close();
      if( totBorrados <= 0 ){
        ret = false;
      }
    }else{
      ret = false;
    }
    return ret;
  }
}